import 'dart:collection';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterdemo/http/http_test.dart';
import 'package:flutterdemo/models/entity/base_entity.dart';
import 'package:flutterdemo/models/entity/user_entity.dart';
import 'package:flutterdemo/utils/toast_util.dart';

class LoginRoute extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new LoginState();
  }
}

class LoginState extends State<LoginRoute> {
  TextEditingController _usernameController = new TextEditingController();
  TextEditingController _passwordController = new TextEditingController();
  FocusNode _usernameFocusNode = new FocusNode();
  GlobalKey _formKey = new GlobalKey<FormState>();

  @override
  void initState() {
    super.initState();
    _usernameController.text = "江渚清沨";
    _passwordController.text = "jerry1990liu";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("登录"),
      ),
      body: Form(
          key: _formKey,
          child: Column(
            children: <Widget>[
              TextFormField(
                autofocus: true,
                controller: _usernameController,
                style: TextStyle(
                  fontSize: 20,
                  color: Colors.green,
                ),
                decoration: InputDecoration(
                  labelText: "用户名",
                  hintText: "请输入用户名",
                  prefixIcon: Icon(Icons.person_outline),
                ),
                focusNode: _usernameFocusNode,
                validator: (v) {
                  return v.trim().length > 0 ? null : "请输入用户名";
                },
              ),
              TextFormField(
                autofocus: false,
                controller: _passwordController,
                style: TextStyle(
                  fontSize: 20,
                  color: Colors.green,
                ),
                decoration: InputDecoration(
                  labelText: "密码",
                  hintText: "请输入密码",
                  prefixIcon: Icon(Icons.lock_outline),
                ),
                obscureText: true,
                validator: (v) {
                  return v.trim().length > 6 ? null : "密码不能少于6位";
                },
              ),
              new Padding(
                padding: EdgeInsets.all(15),
                child: RaisedButton(
                  child: Text("登录"),
                  onPressed: () {
                    if ((_formKey.currentState as FormState).validate()) {
                      ToastUtil.showToastCenter("输入合法");
                      login(_usernameController.text, _passwordController.text);
                    }
                  },
                ),
              ),
              OutlineButton(
                child: Text("http test"),
                onPressed: () {
                  HttpTest().get();
                  HttpTest().post();
                },
              ),
              Row(
                children: <Widget>[
                  OutlineButton(
                    child: Text("dio get"),
                    onPressed: () {
                      DioTest().get();
                    },
                  ),
                  OutlineButton(
                    child: Text("dio post"),
                    onPressed: () {
                      DioTest().post();
                    },
                  ),
                ],
              ),
            ],
          )),
    );
  }

  void login(String account, String password) async {
    var httpClient = new HttpClient();
    var uri = Uri.parse("https://www.wanandroid.com/user/login");
//    var request = await httpClient.post(
//        "https://www.wanandroid.com/", 8080, "user/login");
    var request = await httpClient.postUrl(uri);
//    request.headers.set("Content-Type", "application/json");
    //请求体
    Map map = new HashMap();
    map["username"] = account;
    map["password"] = password;
    request.add(utf8.encode(json.encode(map)));
    HttpClientResponse response = await request.close();
    if (response.statusCode == HttpStatus.ok) {
      var responseBody = await response.transform(utf8.decoder).join();
      print("responseBody:$responseBody");
      BaseEntity<UserEntity> baseEntity =
          new BaseEntity.fromJson(json.decode(responseBody));
      print("baseResponse:${baseEntity.errorMsg}");
    } else {
      print("response.statusCode=${response.statusCode}");
    }
  }
}
